﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Darwin.Source.Components.World.PotentialFeild
{
    public enum FeildType
    {
        Resources = 0,
        Home,

        NumberOf,
    }

    public class CPotentialFeildVector
    {
        private Vector3 m_v3Position;
        private float m_fRadius;
        private float m_fPower;
        private FeildType m_type;
        private UInt32 m_UID;

        static Color[] FeildColours = new Color[(int)FeildType.NumberOf];

        static public void InitColours( )
        {
            FeildColours[0] = Color.Fuchsia;
            FeildColours[1] = Color.Goldenrod;
        }

        public Vector3 Position
        {
            get
            {
                return m_v3Position;
            }

            set
            {
                m_v3Position = value;
            }
        }

        public float Radius
        {
            get
            {
                return m_fRadius;
            }

            set
            {
                m_fRadius = value;
            }
        }

        public float Power
        {
            get
            {
                return m_fPower;
            }

            set
            {
                m_fPower = value;
            }
        }

        public FeildType Type
        {
            get
            {
                return m_type;
            }

            set
            {
                m_type = value;
            }
        }

        public CPotentialFeildVector( UInt32 UID )
        {
            m_v3Position = Vector3.Zero;
            m_fRadius = 0.0f;
            m_fPower = 0.0f;

            m_UID = UID;
        }

        public Vector3 GetForceAtPoint(ref Vector3 v3Point, out float distance)
        {
            Vector3 v3PointToMe = m_v3Position - v3Point;

            if (m_fRadius > 0.0f)
            {
                if (v3PointToMe.LengthSquared() > m_fRadius * m_fRadius)
                {
                    distance = float.MaxValue;
                    return Vector3.Zero;
                }

                float fDistance = v3PointToMe.Length();
                float fScale = fDistance / m_fRadius;

                Vector3 v3PointToMeUnit = v3PointToMe / fDistance;
                
                distance = fDistance;
                return v3PointToMeUnit * (fScale * m_fPower);
            }
            else
            {
                float distanceSquared = v3PointToMe.LengthSquared();
                float fDistance = (float)Math.Sqrt((double)distanceSquared);

                Vector3 v3PointToMeUnit = v3PointToMe / fDistance;

                distance = fDistance;
                if (distanceSquared == 0)
                    return Vector3.Zero;
                return v3PointToMeUnit * ((1.0f / distanceSquared) * m_fPower);
            }
        }

        public void Draw()
        {
#if DEBUG_GRAPHICS_ENABLED
            //
            //  Given a valid XZ value the terrain will return to you a height at that point, and the normal of the triangle you are on
            //
            Vector3 v3Normal;
            m_v3Position.Y = Singleton<SunXNA.Terrain.CTerrain>.Instance.GetHeightAtXZ(m_v3Position, out v3Normal);

            Singleton<SunXNA.DebugGraphics.CManager>.Instance.Add3DObject(m_UID,
                                                                          SunXNA.DebugGraphics.Type.Box,
                                                                          FeildColours[(int)m_type],
                                                                          m_v3Position,
                                                                          v3Normal,
                                                                          new Vector3(m_fRadius, m_fRadius, 0.1f),
                                                                          1.0f);
#endif
        }
    }
}
